home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Sample Code / Interapplication Communication / MenuScripter 4.0 / Sources / MSAECut.c < prev    next >
Encoding:
Text File  |  1996-07-09  |  2.7 KB  |  130 lines  |  [TEXT/CWIE]

  1. // MSAECut.c
  2. //
  3. // Original version by Jon Lansdell and Nigel Humphreys.
  4. // 4.0 and 3.1 updates by Greg Sutton.
  5. // ©Apple Computer Inc 1996, all rights reserved.
  6.  
  7. #include "MSAECut.h"
  8.  
  9. #include "MSAEUtils.h"
  10. #include "MSWindow.h"        // for DPtrFromWindowPtr()
  11.  
  12. #include "MSAESelect.h"
  13.  
  14. #include <Scrap.h>
  15.  
  16.  
  17. #pragma segment AppleEvent
  18.  
  19. // Handle a cut to scrap e.g 'copy last word of document 1'
  20. // If no reference is given then the selection of the front window
  21. // is used.
  22.      
  23. pascal OSErr    DoCut(const AppleEvent *theAppleEvent, AppleEvent *reply, long refcon)
  24. {
  25. #ifdef __MWERKS__
  26.     #pragma unused (reply, refcon)
  27. #endif
  28.  
  29.     AEDesc        directObj = {typeNull, NULL};
  30.     TextToken    aTextToken;
  31.     short        ignore;
  32.     OSErr        err;
  33.  
  34.     err = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard, &directObj);
  35.     // If we get an error here it just means that they haven't supplied a reference to
  36.     // an object to cut - so cut the current section instead.
  37.     
  38.     if (directObj.descriptorType != typeNull)
  39.         err = CutDesc(&directObj);
  40.     else
  41.     {            // Just cut the selection of the front window
  42.         err = GetWindowSelection(FrontWindow(), &aTextToken, &ignore);
  43.         if (noErr != err) goto done;
  44.         
  45.         err = CutTextToken(&aTextToken);
  46.     }
  47.  
  48. done:    
  49.     (void)AEDisposeDesc(&directObj);
  50.         
  51.     return(err);
  52. } // DoCut
  53.  
  54.  
  55. OSErr    CutTextToken(TextToken* theToken)
  56. {
  57.     WindowPtr        aWindow;
  58.     DPtr            docPtr;
  59.     OSErr            err;
  60.     
  61.     aWindow = theToken->tokenWindow;
  62.     docPtr = DPtrFromWindowPtr(theToken->tokenWindow);
  63.     
  64.     if (! aWindow || ! docPtr)
  65.         return(errAENoSuchObject);
  66.  
  67.                     // Set this tokens selection
  68.     err = SelectTextToken(theToken);
  69.     if (noErr != err) goto done;
  70.  
  71.     err = (OSErr)ZeroScrap();
  72.     TECut(docPtr->theText);     
  73.             
  74.     docPtr->dirty = true;
  75.     AdjustScrollbars(docPtr, false);
  76.     DrawPageExtras(docPtr);
  77.     
  78. done:
  79.     return(err);
  80. }
  81.  
  82. OSErr    CutTextDesc(AEDesc* textDesc)
  83. {
  84.     TextToken        aTextToken;
  85.     Size            actualSize;
  86.     OSErr            err;
  87.  
  88.     if (typeMyText != textDesc->descriptorType)
  89.         return(errAETypeError);
  90.         
  91.     GetRawDataFromDescriptor(textDesc, (Ptr)&aTextToken, sizeof(aTextToken), &actualSize);
  92.  
  93.     err = CutTextToken(&aTextToken);
  94.     
  95.     return(err);
  96. }
  97.  
  98. OSErr    CutDesc(AEDesc* aDesc)
  99. {
  100.     AEDesc        cutDesc = {typeNull, NULL},
  101.                 textDesc = {typeNull, NULL};
  102.     OSErr        err;
  103.     
  104.     if (typeObjectSpecifier == aDesc->descriptorType)
  105.         err = AEResolve(aDesc, kAEIDoMinimum, &cutDesc);
  106.     else if (typeNull != aDesc->descriptorType)
  107.         err = AEDuplicateDesc(aDesc, &cutDesc);
  108.         
  109.     if (noErr != err) goto done;
  110.     
  111.     switch (cutDesc.descriptorType)
  112.     {
  113.         case typeAEList:
  114.             err = errAETypeError;
  115.             // We can't handle cutting more than one item to the scrap
  116.             break;
  117.             
  118.         default:
  119.             err = AECoerceDesc(&cutDesc, typeMyText, &textDesc);
  120.             if (noErr != err) goto done;
  121.             err = CutTextDesc(&textDesc);
  122.     }
  123.     
  124. done:
  125.     (void)AEDisposeDesc(&cutDesc);
  126.     (void)AEDisposeDesc(&textDesc);
  127.     
  128.     return(err);
  129. }
  130.